// import './index.scss';
import XHeader from 'vux/src/components/x-header/index.vue'
import Tab from 'vux/src/components/tab/tab.vue'
import TabItem from 'vux/src/components/tab/tab-item.vue'
import Swiper from 'vux/src/components/swiper/swiper.vue'
import SwiperItem from 'vux/src/components/swiper/swiper-item.vue'
import XButton from 'vux/src/components/x-button/index.vue'
import Flexbox from 'vux/src/components/flexbox/flexbox.vue'
import FlexboxItem from 'vux/src/components/flexbox/flexbox-item.vue'
import XProgress from 'vux/src/components/x-progress/index.vue'
import Popup from 'vux/src/components/popup/index.vue'
import Group from 'vux/src/components/group/index.vue'
import Cell from 'vux/src/components/cell/index.vue'
import Checker from 'vux/src/components/checker/checker.vue'
import CheckerItem from 'vux/src/components/checker/checker-item.vue'
import XInput from 'vux/src/components/x-input/index.vue'
import XTextarea from 'vux/src/components/x-textarea/index.vue'
import TransferDom from 'vux/src/directives/transfer-dom/index'
import Alert from 'vux/src/components/alert/index.vue'
import VueQr from 'vue-qr'
import inlineLoading from 'vux/src/components/inline-loading/index.vue'
import Checklist from 'vux/src/components/checklist/index.vue'
import wx from 'weixin-js-sdk'


import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
// import Http from "../../common/http";

export default {
  name: 'Project',
  directives: {
    TransferDom
  },
  components: {
    XHeader,
    Tab,
    TabItem,
    Swiper,
    SwiperItem,
    XButton,
    Flexbox,
    FlexboxItem,
    XProgress,
    Popup,
    Group,
    Cell,
    Checker,
    CheckerItem,
    XInput,
    XTextarea,
    Checklist,
    Alert,
    VueQr,
    inlineLoading

  },
  data () {
    return {
      index: 0,
      // 信息反馈是否有红点  为''的时候没有  为' '的时候有
      isXinxi: ' ',
      fankui_btn_text: '我要捐款',
      yinzhangShow: false,
      percent1: 60,
      show: false,
      showImgBox:false,
      imgSrc: '',
      demo: 10,
      isM: '募捐中',
      userId: '',
      eventType: '',
      feedBack: [],
      amountConfig:[],
      name: '',
      mobile: '',
      IDNum: '',
      liuyan: '',
      ifShowtt: false,
      address: '',

      item: 'item',
      textMoney: '',
      checkOK: true,
      isA: true,
      threshold: 50,
      minmovingdistance: 30,
      // 项目id
      event_id: '',
      // 显示哪一个模块
      witchShow: 'xiangqing',
      showLoading:false,
      pageNoOther: 1,
      pageNoJuankuan: 1,
      pageSize: 20,
      eventAll: {},
      eventOther: [],
      eventJuankuan: [],
      receiptRankTop3: [],
      lookMore: '查看更多',
      lookMoreJuankuans: '查看更多',
      leaveMessage: [],
      pageNoLeave: 1,
      isVolunteer: false,
      ifselected: false,
      configSuccess: false,
      qrcodeImg: '',
      showAlert: false,
      titleAlert: '长按二维码保存',
      logoSrc: 'http://www.qqchou.org/userfiles/2/images/1513911875.png',
      dotScale: 1,
      logoMargin: 5,
      margin: 0,
      logoCornerRadius: 4,
      isID: function (value) {
        let r = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
          if (r.test(value)) {
          return {
            valid: true,
            msg: '身份证正确'
          }
        } else {
          return {
            valid: false,
            msg: '身份证错误'
          }
        }
      }
    }
  },
  watch: {
    textMoney () {
      // this.demo = this.textMoney * 1
      // if (this.demo != 10 && this.demo != 20 && this.demo != 50 && this.demo != 100 && this.demo != 1000 && this.textMoney == this.demo) {
      //   this.ifselected = true
      // } else {
      //   this.ifselected = false
      // }
    }
  },
  mounted () {
    let openid = sessionStorage.getItem("openid")
    if (openid == null || openid == '' || openid == 'undefined') {
      sessionStorage.setItem('openid', this.$route.query.openId)
      sessionStorage.setItem('userId', this.$route.query.userId)
      sessionStorage.setItem('wxAvatar', this.$route.query.wxAvatar)
      sessionStorage.setItem('nickName', this.$route.query.nickName)
      sessionStorage.setItem('event_id', this.$route.query.event_id)
      sessionStorage.setItem('choice',this.$route.query.choice)

      // console.log(window.sessionStorage.getItem('openid'))
      let openid = sessionStorage.getItem("openid")
      if (openid == null || openid == '' || openid == 'undefined') {
        // alert('进行页面的跳转')
        window.location.href = 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.$route.query.event_id
      }
    }
    this.userId = window.sessionStorage.getItem('userId')
    this.event_id = this.$route.query.event_id;
    //alert(this.$route.query.event_id+'query----')
    this.$emit('ifOpenid', this.$route.path, this.event_id)
    if (this.$route.query.openId) {
      window.location.reload()
    }
    // console.log(this.event_id)



    http.post(CONSTANT.EVENT_VIEW, {
      event_id: this.event_id,
      userId: this.userId
      // pageNo: this.pageNo,
      // pageSize: this.pageSize
    }, (res) => {
      console.log('event', res)
      if (res.code == 200) {
        this.eventAll = res.obj
        if (this.eventAll.raiseMoney >= this.eventAll.eventGoalAmount) {
          this.imgSrc = 'http://tsw-qqc.oss-cn-hangzhou.aliyuncs.com/userfiles/2018/12/1545390254.png'
          this.yinzhangShow = true
          this.$nextTick(() => {
            let clientWidth =   `${document.documentElement.clientWidth}`
            document.getElementById("yinzhangStyle").style.left = (clientWidth-160) + 'px'
          let clientHeight =   `${document.documentElement.clientHeight}`
          document.getElementById("yinzhangStyle").style.top = clientHeight*0.312 + 'px'
        })
        } else if (this.eventAll.eventStatus == 4){
          this.yinzhangShow = true
          this.$nextTick(() => {
            let clientWidth =   `${document.documentElement.clientWidth}`
            document.getElementById("yinzhangStyle").style.left = (clientWidth-160) + 'px'
          let clientHeight =   `${document.documentElement.clientHeight}`
          document.getElementById("yinzhangStyle").style.top = clientHeight*0.312 + 'px'
        })
          this.imgSrc = 'http://tsw-qqc.oss-cn-hangzhou.aliyuncs.com/userfiles/2018/12/1545390218.png'
        }
        if (this.$route.query.choice == 'feedback') {
          this.witchShow = 'fankui'
          if (this.eventAll.eventStatus == 1) {
            this.fankui_btn_text = '再次捐款'
          }
          this.index = 3
          http.post(CONSTANT.FEEDBACK, {event_id: this.event_id}, (res) => {
            // console.log(res)
            if (res.code == 200) {
            this.feedBack = res.obj.feedBack
          }
        })
        }
        if (this.eventAll.amountConfig != null && this.eventAll.amountConfig != '') {
            this.amountConfig = this.eventAll.amountConfig.split("|")
        } else {
          this.amountConfig = [10, 20, 50, 100, 1000]
        }
        this.demo = this.amountConfig[0]
        this.eventAll.eventContent=this.eventAll.eventContent.replace(/http:\/\/www.qqchou.org/g,'');
        this.eventAll.eventContent=this.eventAll.eventContent.replace(/src="/g,'src="http://tsw-qqc.oss-cn-hangzhou.aliyuncs.com');
        //if (this.event_id ==  'd6bc8b1b17ff4861aca578e97673b981') {
        //  var url = encodeURIComponent("http://mp.weixin.qq.com/s/PMVM13misiJXHzOf3HcAng");
        //  this.$emit('ShareAppMessage', '接力！一元钱压岁钱！给更多的小伙伴带来欢笑~', '“捐出一元压岁钱，快乐公益我助力”——为“浙江红领巾公益基金”募捐行动', 'http://www.qqchou.org/a/wxinterface/toShare?url='+url,'http://www.qqchou.org/userfiles/2018/3/20180321165239.jpg')
        //} else {
        //  this.$emit('ShareAppMessage', this.eventAll.eventName, '【亲青筹】' + this.eventAll.eventAbstract, 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.event_id, this.imageUrl.imageUrl + this.eventAll.eventPicUrl)
        //}
        this.$emit('ShareAppMessage', this.eventAll.eventName, '【亲青筹】' + this.eventAll.eventAbstract, 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.event_id, this.imageUrl.imageUrl + this.eventAll.eventPicUrl)
        this.qrcodeImg = 'http://www.qqchou.org/a/wxinterface/wxOAuth?category=3&event_id=' + this.event_id
        // if (res.obj.feedBack == '') {
        //   this.isXinxi = ''
        // }
        if (res.obj.isVolunteer == 0 || res.obj.isVolunteer == '') {
          this.isVolunteer = false
        } else if (res.obj.isVolunteer == 1 && res.obj.eventStatus == 1) {
          this.isVolunteer = true
        }
        if (res.obj.eventStatus == 1) {
          this.isM = '募捐中'
        } else if (res.obj.eventStatus == 4) {
          this.isM = '已结束'
        }
        if (res.obj.eventType == 'pool') {
          this.eventType = '助困'
        } else if (res.obj.eventType == 'other') {
          this.eventType = '其它'
        } else if (res.obj.eventType == 'study') {
          this.eventType = '学习'
        } else if (res.obj.eventType == 'medicine') {
          this.eventType = '助医'
        }
        this.name = res.obj.name
        //alert(this.name)
        this.mobile = res.obj.telephone
        this.IDNum = res.obj.idCard
        this.address = res.obj.address
        if (res.obj.name == '' || res.obj.telephone == '' || res.obj.idCard == '') {
          this.ifShowtt = true
        }
        if (this.event_id == 'd6bc8b1b17ff4861aca578e97673b981') {
          this.ifShowtt = false
          this.showImgBox = true
        }
        this.$nextTick(function () {
          var detailsContent = document.getElementById('detailsContent')
          var detailsContentImages = detailsContent.getElementsByTagName('img')
          var imagesArr = []
          var currimg = ''
          for (var i = 0; i < detailsContentImages.length; i++) {
            imagesArr.push(detailsContentImages[i].src)
            detailsContentImages[i].onclick = function () {
              currimg = this.src
              wx.previewImage({
                current: currimg, // 当前显示图片的http链接
                urls: imagesArr // 需要预览的图片http链接列表
              })
            }
          }
        })
        this.leaverMessage()
      }
    })
    // this.OtherProjects();
    // this.OtherJuankuans();
  },
  updated () {
    // let thisPageUrl = window.location.href.split('#')[0]
    // // console.log(thisPageUrl)
    // console.log('微信授权url****' + thisPageUrl)
    // http.post(CONSTANT.WXCONFIG, {'url': thisPageUrl}, (res) => {
    // this.configSuccess = true
    //   if (res.code == 200) {
    //     console.log('config参数已返回')
    //     this.wx.config({
    //       "debug": false,
    //       "appId": res.obj.appid,
    //       "timestamp": res.obj.timestamp,
    //       "nonceStr": res.obj.nonceStr,
    //       "signature": res.obj.signature,
    //       "jsApiList": ['previewImage']
    //     })
    //   }
    // })
  },
  watch: {
    '$route.query.event_id': function () {
      // pass
      //alert('参数变化了哦')
      location.reload()
    }
  },
  methods: {
    // 信息反馈
    fankui () {
      this.witchShow = 'fankui'
      if (this.feedBack.length < 1 ) {
        this.tofeedBack()
      }
    },
    // 点击发起单位
    faqidanwei () {
      this.witchShow = 'danwei'
      if (this.eventOther.length < 1) {
        this.OtherProjects()
      }
    },
    // 点击捐款记录
    juankuanjilu () {
      this.witchShow = 'jilu'
      if (this.eventJuankuan.length < 1) {
        this.OtherJuankuans()
      }
    },
    // 项目反馈获取
    tofeedBack () {
      http.post(CONSTANT.FEEDBACK, {event_id: this.event_id}, (res) => {
        // console.log(res)
        if (res.code == 200) {
          this.feedBack = res.obj.feedBack
        }
      })
    },
    goHello () {
      this.$router.push({path: '/Hello'})
    },
    comeBack () {
      //if (this.$route.query.event_id && this.event_id != this.$route.query.event_id) {
      //  window.sessionStorage.setItem('event_id', this.$route.query.event_id)
      //  window.history.go(0)
      //  return
      //}
      let choice = this.$route.query.choice
      if (choice == 'feedback' || choice == 'memorandum') {
        //window.location.href = 'http://www.qqchou.org/a/wxinterface/wxOAuth'
        this.$router.push({path: '/Hello'})
      } else {
        window.history.go(-1)
      }
    },
    goProjectDetails (eventId) {
      window.sessionStorage.setItem('event_id', eventId)
      this.$router.push({path: '/Project', query: {event_id: eventId}})
    },
    leaverMessage () {
      // console.log(this.pageNoLeave)
      http.post(CONSTANT.COMMENT_LIST, {
        event_id: this.event_id,
        pageNo: this.pageNoLeave,
        pageSize: this.pageSize
      }, (res) => {
        // console.log(res, 'liuyan')
        if (res.code == 200) {
          if (res.errmsg == '没有更多的留言') {
            if (this.pageNoLeave > 1) {
              this.pageNoLeave--
              alert('没有更多的留言了！')
            }
            return
          }
          if (this.pageNoLeave == 1) {
            this.leaveMessage = res.list
          } else {
            this.leaveMessage = this.leaveMessage.concat(res.list)
          }
        }
      })
    },
    moreLeaverMessage () {
      this.pageNoLeave++
      this.leaverMessage()
    },
    moreOtherProjects () {
      // 查看更多该单位项目
      this.pageNoOther++
      this.OtherProjects()
    },
    OtherProjects () {
      // 单位发起的项目
      http.post(CONSTANT.EVENT_ORGANIZE, {
        event_id: this.event_id,
        pageNo: this.pageNoOther,
        pageSize: 5
      }, (res) => {
        // console.log('发起项目', res)
        if (res.code == 200) {
          if (res.errmsg == '没有更多的项目') {
            this.pageNoOther--
            this.lookMore = '没有更多的项目了'
            return
          } else if (res.errmsg == '') {
            if (this.pageNoOther == 1) {
              this.eventOther = res.list
            } else {
              this.eventOther = this.eventOther.concat(res.list)
            }
          }
        }
      })
    },
    moreOtherJuankuans () {
      // 查看更多捐款
      this.pageNoJuankuan++
      this.OtherJuankuans(1)
    },
    OtherJuankuans (isMore) {
      if (isMore == 1) {
        this.showLoading = true
      }
      http.post(CONSTANT.EVENT_DONATE, {
        event_id: this.event_id,
        pageNo: this.pageNoJuankuan,
        pageSize: this.pageSize
      }, (res) => {
        //console.log(res)
        this.showLoading = false
      if (res.errmsg == '没有更多的项目') {
        this.pageNoJuankuan--
        this.lookMoreJuankuans = '没有更多的捐款记录了'
        return
      } else if (res.errmsg == '') {
        if (this.pageNoJuankuan == 1) {
          this.eventJuankuan = res.list
        } else {
          this.eventJuankuan = this.eventJuankuan.concat(res.list)
        }
      }
    })
    },
    isShow () {
      // 弹出框关闭按钮
      this.show = false
    },
    wantJuank () {
      // 我要捐款
      this.show = true
    },
    keyups () {
      this.demo = this.textMoney
      this.ifselected = true
    },
    blurs () {
      this.ifselected = false
    },
    checkerWitch () {
      // 选择捐款金额
      // console.log(this.demo)
    },
    checkOk () {
      // 是否同意用户协议
      // console.log(this.checkOK)
    },
    textMoneyChange () {
      // console.log('hh')
    },
    goreport () {
      this.$router.push({path: '/report'})
    },
    godonation () {
      this.$router.push({path: '/donation',query:{event_id:this.event_id}})
    },
    govolunteer () {
      this.$router.push({
        path: '/volunteer',
        query: {volunteerLocal: this.eventAll.volunteerLocal, volunteerTime: this.eventAll.volunteerTime}
      })
    },
    primary () {
      console.log("***********立刻捐款11***************")
      let num = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
      let _this = this
      if (_this.demo == '') {
        alert('请选择捐款金额!')
      } else if (!num.test(_this.demo)) {
        alert('请输入正确的金额数!（提示：只能是数字或者精确到两位小数）')
      } else if (_this.demo == 0 || _this.demo == '0.00') {
        alert('请输入正确的金额数!（提示：只能是数字或者精确到两位小数）')
      } else if (this.checkOK == false) {
        alert('请同意并接受\n《亲青筹用户协议》！')
      } else {
        // window.location.href = 'http://www.qqchou.org/a/wxinterface/payment?' + 'witchOne=geren&userId=' +
        //   this.userId + '&money=' + this.demo + '&eventId=' + this.event_id
        //alert(this.name)
        this.$router.push({
          path: '/payment',
          query: {
            witchOne: 'geren',
            money: this.demo,
            name: this.name,
            idCard: this.IDNum,
            telephone: this.mobile,
            address: this.address,
            content: this.liuyan,
            event_id: this.event_id
          }
        })
      }
    },
    goTogotherRecord () {
      this.$router.push({path: '/togetherDonate', query: {event_id: this.event_id}})
    },
    gofindRandSingle () {
      this.$router.push({path: '/findranksingle', query: {event_id: this.event_id}})
    }
  }
}
// else if (_this.name == '') {
//   alert('请输入您的姓名！')
// } else if (_this.mobile == '') {
//   alert('请输入您的手机号！')
// } else if (_this.$refs.mobile.valid === false) {
//   alert('请输入正确的手机号')
// } else if (_this.IDNum == '') {
//   alert('请输入您的身份证号码！')
// } else if (_this.$refs.IDNum.valid === false) {
//   alert('请输入正确的身份证号码！')
// }
